<include file="Public/head"/>
<link rel="stylesheet" href="/static/kindeditor/themes/default/default.css"/>
<link rel="stylesheet" href="/static/kindeditor/plugins/code/prettify.css"/>
<script charset="utf-8" src="/static/kindeditor/kindeditor.js"></script>
<script charset="utf-8" src="/static/kindeditor/lang/zh_CN.js"></script>
<script charset="utf-8" src="/static/kindeditor/plugins/code/prettify.js"></script>
<script>
    var editor1, editor2;
    KindEditor.ready(function (K) {
        editor1 = K.create('#activity_intro', {
            cssPath: '/static/kindeditor/plugins/code/prettify.css',
            uploadJson: '/Article/ueditor',
            allowFileManager: false,
            height: '200px'
        });
        editor2 = K.create('#activity_rule', {
            cssPath: '/static/kindeditor/plugins/code/prettify.css',
            uploadJson: '/Article/ueditor',
            allowFileManager: false,
            height: '200px'
        });
        prettyPrint();
    });
</script>
<style>
    .newly-build ul .upload-type .genghua {
        display: none;
    }
</style>
<include file="Public/common_header"/>
<div class="mian">
    <include file="Public/sidebar" menu="clcwht.wdhdgl"/>

    <div class="mianR">
        <h3 class="yonghu">活动管理 > 添加活动</h3>
        <div class="newly-build">
            <ul>
                <li>
                    <span><i class="must">*</i>活动名称</span>
                    <input type="text" placeholder="请输入活动名称" name="activity_name" id="activity_name"
                           value="{$activity.activity_name}" class="build-title" maxlength="50"/>
                </li>
                <li class="upload-type">
                    <span><i class="must">*</i>活动图片</span>
                    <input type="file"/>
                    <script type='text/javascript'>
                        new IFrameUploadImg(
                            '浏览'
                            , 'http://' + window.location.host + '/Activity/uploadPic'
                            , function (res) {
                                var cdn_url = '{$t|get_content_cdnurl}';
                                if (res.code == 200) {
                                    var head_url = res.data[0];
                                    $('#app_img').html('<img src="' + cdn_url + head_url + '@80h_90Q' + '" style="position: relative;margin-top: -10px;"/>');
                                    $("#activity_app_image").val(head_url);
                                } else {
                                    shibai_alert(res.msg);
                                }
                            });
                    </script>
                    <div class="build-map" id="app_img" style="font-size: 14px;">
                        <if condition="$activity['activity_app_image']">
                            <img src="{$t|get_content_cdnurl}{$activity.activity_app_image}"/>
                            <else/>
                            添加移动端图片
                        </if>
                    </div>
                    <input type="hidden" id="activity_app_image" style="position: relative;margin-top: -10px;"/>
                </li>
                <li class="upload-type">
                    <input type="file"/>
                    <script type='text/javascript'>
                        new IFrameUploadImg(
                            '浏览'
                            , 'http://' + window.location.host + '/Activity/uploadPic'
                            , function (res) {
                                var cdn_url = '{$t|get_content_cdnurl}';
                                if (res.code == 200) {
                                    var head_url = res.data[0];
                                    $('#web_img').html('<img src="' + cdn_url + head_url + '@80h_90Q' + '" style="position: relative;margin-top: -10px;"/>');
                                    $("#activity_pc_image").val(head_url);
                                } else {
                                    shibai_alert(res.msg);
                                }
                            });
                    </script>
                    <div class="build-map" id="web_img" style="font-size: 14px;">
                        <if condition="$activity['activity_pc_image']">
                            <img src="{$t|get_content_cdnurl}{$activity.activity_pc_image}"
                                 style="position: relative;margin-top: -10px;"/>
                            <else/>
                            添加桌面端图片
                        </if>
                    </div>
                    <input type="hidden" id="activity_pc_image" style="position: relative;margin-top: -10px;"/>
                </li>
                <li>
                    <span><i class="must">*</i>活动专家</span>
                    <div class="expert">
                        <div class="ex-text">
                            <input type="text" class="userId" placeholder="请输入用户ID" maxlength="10">
                            <span class="query">查询</span>
                            <div class="experts">
                                <em></em>
                                <input type="text" maxlength="20" placeholder="请输入用户简介，不多于20字符">
                            </div>
                        </div>
                        <div class="ex-text">
                            <input type="text" class="userId" placeholder="请输入用户ID" maxlength="10">
                            <span class="query">查询</span>
                            <div class="experts">
                                <em></em>
                                <input type="text" maxlength="20" placeholder="请输入用户简介，不多于20字符">
                            </div>
                        </div>
                        <div class="ex-text">
                            <input type="text" class="userId" placeholder="请输入用户ID" maxlength="10">
                            <span class="query">查询</span>
                            <div class="experts">
                                <em></em>
                                <input type="text" maxlength="20" placeholder="请输入用户简介，不多于20字符">
                            </div>
                        </div>
                        <div class="ex-text">
                            <input type="text" class="userId" placeholder="请输入用户ID" maxlength="10">
                            <span class="query">查询</span>
                            <div class="experts">
                                <em></em>
                                <input type="text" maxlength="20" placeholder="请输入用户简介，不多于20字符">
                            </div>
                        </div>
                    </div>
                </li>
                <li>
                    <span><i class="must">*</i>活动简介</span>
                    <textarea id="activity_intro" placeholder="请输入活动简介">{$activity.activity_intro}</textarea>
                </li>
                <li>
                    <span><i class="must">*</i>活动规则</span>
                    <textarea id="activity_rule" placeholder="请输入活动规则">{$activity.activity_rule}</textarea>
                </li>
            </ul>
        </div>
        <div class="yongBtn">
            <input type="button" value="保存" class="queding">
            <input type="button" onclick="history.go(-1);" value="取消" class="quxiao">
        </div>

        <div class="yunBei"></div>
        <script type="text/javascript">
            $(function () {
                var cdnUrl = '{$t|get_content_cdnurl}';
                var activityPicImage = $("#activity_pc_image").val();
                if (activityPicImage != '') {
                    $('#web_img').html('<img src="' + cdnUrl + activityPicImage + '@80h_90Q' + '" style="position: relative;margin-top: -10px;"/>');
                }
                var activityAppImage = $("#activity_app_image").val();
                if (activityAppImage != '') {
                    $('#app_img').html('<img src="' + cdnUrl + activityAppImage + '@80h_90Q' + '" style="position: relative;margin-top: -10px;"/>');
                }
                $(".expert .query").each(function (i) {
                    var curentUid = $(this).prev('.userId').val();
                    var currentUtro = $(this).prev('.userId').siblings(".experts").find("input").val();
                    if (curentUid > 0 && currentUtro != '') {
                        checkExpert($(this), curentUid);
                    }
                    $(this).prev('.userId').bind('input propertychange change', function () {
                        var userId = $(this).val();
                        if (userId == '') {
                            $(this).siblings(".experts").hide();
                            $(this).siblings(".experts").find("input").val("");
                        }
                    });
                    $(this).on("click", function () {
                        var userId = $.trim($(this).siblings('.userId').val());
                        if (userId == '') {
                            shibai_alert('抱歉，用户ID不能为空');
                            return false;
                        }
                        var thisObj = $(this);
                        checkExpert(thisObj, userId)
                    })
                })
            });

            function checkExpert(thisObj, userId) {
                $.ajax({
                    type: 'post',
                    url: '/ClcwUser/checkClcwExpertUser',
                    data: 'userId=' + userId,
                    async: false,
                    dataType: 'JSON',
                    success: function (data) {
                        if (data.code != 200) {
                            shibai_alert(data.msg);
                            return false;
                        }
                        thisObj.next('.experts').find('em').text("牛人昵称：" + data.data.nick_name)
                        thisObj.next(".experts").show();
                    }
                });
            }


            $(".yongBtn .queding").on("click", function () {
                editor1.sync();
                editor2.sync();
                var activity_name = $("#activity_name").val();
                if (activity_name == '') {
                    shibai_alert('提交失败，请填写活动名称');
                    return false;
                }
                var activity_pc_image = $("#activity_pc_image").val();
                if (activity_pc_image == '') {
                    shibai_alert('提交失败，请添加桌面端图片');
                    return false;
                }
                var activity_app_image = $("#activity_app_image").val();
                if (activity_app_image == '') {
                    shibai_alert('提交失败，请添加移动端图片');
                    return false;
                }
                var str = "";
                var arrId = [];//ID值数组
                var arrText = [];//简介值数组
                var isReturn = true;
                $(".expert .userId").each(function () {
                    str += $(this).val();
                    arrId.push($(this).val());
                });
                if (str == "") {
                    shibai_alert("提交失败，请填写至少1名专家");
                    isReturn = false;
                    return false;
                }
                $(".ex-text").each(function () {
                    if ($(this).find("input").eq(0).val() != "") {
                        if ($(this).find("input").eq(1).val() == "") {
                            shibai_alert("提交失败，请填写专家简介");
                            isReturn = false;
                            return false;
                        }
                    }
                    arrText.push($(this).find("input").eq(1).val())
                });
                if (isReturn) {
                    arrIds = trimSpaceArrId(arrId);
                    arrTexts = trimSpaceArrText(arrText);
                    var activity_intro = $("#activity_intro").val();
                    if (activity_intro == '') {
                        shibai_alert('提交失败，请填写活动简介');
                        return false;
                    }
                    var activity_rule = $("#activity_rule").val();
                    if (activity_rule == '') {
                        shibai_alert('提交失败，请填写活动规则');
                        return false;
                    }
                    var params = {
                        'activity_name': activity_name,
                        'activity_pc_image': activity_pc_image,
                        'activity_app_image': activity_app_image,
                        'activity_expert_uids': arrIds,
                        'activity_expert_intros': arrTexts,
                        'activity_intro': activity_intro,
                        'activity_rule': activity_rule
                    }
                    $.post("/Activity/addActivityData", params, function (res) {
                        if (res.code == 200) {
                            btn_alert(res.msg);
                            setTimeout(function () {
                                window.location.href = "/Activity/index";
                            }, 2000);
                        } else {
                            shibai_alert(res.msg);
                        }
                    }, 'json');
                }

            });
            function trimSpaceArrId(arrId) {
                for (var i = 0; i < arrId.length; i++) {
                    if (arrId[i] == "" || typeof(arrId[i]) == "undefined") {
                        arrId.splice(i, 1);
                        i = i - 1;
                    }
                }
                return arrId;
            }


            function trimSpaceArrText(arrText) {
                for (var i = 0; i < arrText.length; i++) {
                    if (arrText[i] == "" || typeof(arrText[i]) == "undefined") {
                        arrText.splice(i, 1);
                        i = i - 1;
                    }
                }
                return arrText;
            }
        </script>
        <include file="Public/foot"/>
